package com.example.demo.util;

/**
 * @author 成斌
 * @date 2022/10/23
 */
public class ResData<T> {

    public enum CodeEnum {
        SUCCESS("200"),
        CREATE_UPDATE_SUCCESS("201"),
        DELETE_SUCCESS("204"),
        FAIL("502");

        private String code;

        public String getCode() {
            return code;
        }

        CodeEnum(String code) {
            this.code = code;
        }
    }

    protected String code;

    protected String msg;

    protected T data;

    public ResData() {
    }

    public ResData(T data, String code) {
        this.data = data;
        this.code = code;
    }

    public ResData(T data, String code, String msg) {
        this.data = data;
        this.code = code;
        this.msg = msg;
    }

    public static <T> ResData<T> success(T data) {
        return new ResData(data, CodeEnum.SUCCESS.getCode());
    }

    public static <T> ResData<T> success(T data, String msg) {
        return new ResData(data, CodeEnum.SUCCESS.getCode(), msg);
    }

    public static <T> ResData<T> createSuccess(T data) {
        return new ResData(data, CodeEnum.CREATE_UPDATE_SUCCESS.getCode());
    }

    public static <T> ResData<T> createSuccess(T data, String msg) {
        return new ResData(data, CodeEnum.CREATE_UPDATE_SUCCESS.getCode(), msg);
    }

    public static <T> ResData<T> updateSuccess(T data) {
        return new ResData(data, CodeEnum.CREATE_UPDATE_SUCCESS.getCode());
    }

    public static <T> ResData<T> updateSuccess(T data, String msg) {
        return new ResData(data, CodeEnum.CREATE_UPDATE_SUCCESS.getCode(), msg);
    }

    public static <T> ResData<T> deleteSuccess() {
        return new ResData((Object) null, CodeEnum.DELETE_SUCCESS.getCode());
    }

    public static <T> ResData<T> deleteSuccess(String msg) {
        return new ResData((Object) null, CodeEnum.DELETE_SUCCESS.getCode(), msg);
    }

    public static <T> ResData<T> failed() {
        return new ResData((Object) null, CodeEnum.FAIL.getCode());
    }

    public static <T> ResData<T> failed(String msg) {
        return new ResData((Object) null, CodeEnum.FAIL.getCode(), msg);
    }


    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResData{code='" + this.code + '\'' + ", msg='" + this.msg + '\'' + ", data=" + this.data + '}';
    }
}
